This sample code will teach you the basic of creating a thread in C#. The console app that we are about to create will a new ThreatStart delegate that will point to a method which will be executed by the thread. The delegate will be passed as a parameter whenever a new thread instance is created. Then, the Thread.Start will be called to run the method.
Below is the source code of what we have explain about creating a Thread in C#:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; class program { public void CreateThreadProgram() { for (int i = 0; i < 10; i++) { Console.WriteLine("Thread is Active"); } } } class callThreadProg { public static void Main() { program _program = new program(); Thread _thread = new Thread(new ThreadStart(_program.WorkThreadFunction)); _thread.Start(); Console.Read(); } }
Now this is the output of the C# Program:
Thread is ActiveThread is ActiveThread is ActiveThread is ActiveThread is ActiveThread is Active
Comments